package cn.wsalix.shop.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import cn.anlaser.entity.SimpleEntity;
import cn.wsalix.biz.entity.Category;
import cn.wsalix.shop.status.ProductEnum;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "PRODUCT")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@EntityListeners({ AuditingEntityListener.class })
@Data
@EqualsAndHashCode(exclude = { "parentCat", "category", "prices", "brandItem",
		"originItem" }, callSuper = false)
@ToString(exclude = { "parentCat", "category", "prices", "brandItem",
		"originItem" })
public class Product extends SimpleEntity {
	/**
		 * 
		 */
	private static final long serialVersionUID = 8500867520550921062L;
	private String brand;
	private String brandLogo;// 品牌
	private String origin;// chan产地
	private String originLogo;// chanLogo

	@Column(columnDefinition = "int default 0", nullable = false)
	private Integer priceSrc;// 价格
	@Column(columnDefinition = "int default 0", nullable = false)
	private Integer price;// 价格
	@Column(columnDefinition = "int default 0", nullable = false)
	private Integer priceTuan;// 团购价格

	private String discount;// preferential;//优惠
	private String unit;// 单位
	@Column(columnDefinition = "int default 0", nullable = false)
	private Integer count;// 产品数量
	private Integer saleNum;// 已售商品数量
	private int level;// 级别
	private int start;// 星级
	private String remark;// 产品描述
	private String area;//
	private ProductEnum status;// 备注

	private boolean tuan;// 是否团购
	private int tuanNum;
	@Column(columnDefinition = "int default 0", nullable = false)
	private Integer postfree;// 包邮
	private Integer store;// 0无货、1现货

	private String type;// 产品类新,新品/二手
	private String url;// h5页
	private String tags;
	private String intro;// 标题
	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(name = "content", columnDefinition = "TEXT", nullable = true, length = 100000)
	private String content;// 产品详情
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_PRODUCT_SHOP"), name = "SHOP_ID")
	private Shop shop;// 店铺
	@JsonIgnore
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_PRODUCT_CATEGORY"), name = "CAT_ID")
	private Category category;
	// private Long catId;
	@JsonIgnore
	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_PRODUCT_PARENT_CATEGORY"), name = "PARENT_CAT_ID")
	private Category parentCat;
	private String logo;
	/*
	 * @ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch =
	 * FetchType.LAZY)
	 * 
	 * @JoinColumn(name = "PRICE_ID") private ProductPrice prices;
	 */

	@OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY, mappedBy = "product")
	private Set<Price> prices;

	/*
	 * @OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY, mappedBy
	 * = "product") private Set<UploadFile> images;
	 */
	/**
	 * 
	 */

	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_PRODUCT_BRAND"), name = "BRAND_ID")
	private Brand brandItem;

	@ManyToOne(cascade = CascadeType.MERGE, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(foreignKey = @ForeignKey(name = "FK_PRODUCT_ORIGIN"), name = "ORIGIN_ID")
	private Origin originItem;

	@Temporal(TemporalType.DATE)
	private Date workDate;
	private String httpUrl;// 商品展示页
	@Transient
	private boolean select = false;
}